package com.jblv.frame.config.controller;

import com.alibaba.fastjson.JSONObject;
import com.jblv.frame.config.ConfigCenterApplication;
import com.jblv.frame.config.bean.AppInfo;
import com.jblv.frame.config.service.ConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title ConfigController
 * @projectName jblv-frame
 * @packageName com.jblv.frame.config.controller
 * @description 基础配置Controller
 * @date Create at 2020/9/7 15:27
 * @since jdk1.8
 */
@RestController
@RequestMapping({ "/config" })
public class ConfigController {

    private final static Logger logger = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    ConfigService configService;

    @RequestMapping(value = { "/getAppList/{fileName}" }, method = { RequestMethod.GET })
    public List<AppInfo> getAppList(@PathVariable final String fileName) throws Exception {
        return configService.getAppList(fileName);
    }

    @RequestMapping(value = { "/system/{fileName}" }, method = { RequestMethod.GET })
    public Object getSystemConfig(@PathVariable final String fileName) throws Exception {
        return configService.getConfigFromXML(fileName);
    }

    @RequestMapping(value = { "{host}/software/{fileName}" }, method = { RequestMethod.GET })
    public Object getSoftwareConfig(@PathVariable final String host, @PathVariable final String fileName) throws Exception {
        final JSONObject xml = this.configService.getConfigFromXML(fileName);
        final AppInfo app = this.configService.getAppList(host, xml);
        if (app == null) {
            logger.error("Host [" + host + "] application id could not be found");
            return null;
        }
        final String appId = app.getId();
        return this.configService.getConfigFromDB(appId, host);
    }

    @RequestMapping(value = { "{host}/{fileName}" }, method = { RequestMethod.GET })
    public Object getConfig(@PathVariable final String host, @PathVariable final String fileName) throws Exception {
        final JSONObject json = new JSONObject(true);
        final JSONObject xml = this.configService.getConfigFromXML(fileName);
        json.putAll(xml);
        final AppInfo app = configService.getAppList(host, xml);
        if (app == null) {
            logger.error("Host [" + host + "] application id could not be found");
            return null;
        }
        final String appId = app.getId();
        final JSONObject mongo = this.configService.getConfigFromDB(appId, host);
        json.putAll(mongo);
        final JSONObject yml = configService.getConfigFromYml();
        json.putAll(yml);
        return json;
    }

}
